package com.kylexy.closes.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kylexy.closes.R;
import com.kylexy.closes.app.UIHelper;
import com.kylexy.closes.bean.MainMenu;

public class MainGridAdapter extends BaseAdapter {
	List<MainMenu> mDatas;
	LayoutInflater mInflater;
	// GridView mGridview;
	int mTextRow = 1; // 文字自动换行造成最后一列显示不全
	Context mContext;

	public MainGridAdapter(Context ctx, List<MainMenu> datas) {
		mInflater = LayoutInflater.from(ctx);
		mDatas = datas;
		mContext = ctx;
		// mGridview = gridview;

		for (MainMenu menu : datas) {
			if (menu.getTitle().length() > 4) {
				mTextRow = 2;
				break;
			}
		}

	}

	@Override
	public int getCount() {
		return mDatas.size();

	}

	@Override
	public MainMenu getItem(int position) {

		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final View v;
		v = mInflater.inflate(R.layout.layout_main_grid_item, parent, false);

		ImageView iv = (ImageView) v.findViewById(R.id.main_tab_item_icon);
		TextView tv = (TextView) v.findViewById(R.id.main_tab_item_title);

		final MainMenu data = getItem(position);

		int mod = (position % 4);
		if (mod == 0) {
			iv.setBackgroundResource(R.drawable.icon_1_n_s);
		} else if (mod == 1) {
			iv.setBackgroundResource(R.drawable.icon_2_n_s);
		} else if (mod == 2) {
			iv.setBackgroundResource(R.drawable.icon_3_n_s);
		} else if (mod == 3) {
			iv.setBackgroundResource(R.drawable.icon_4_n_s);
		}

		tv.setMinLines(mTextRow);
		tv.setText(data.getTitle());

		// int height = mGridview.getMeasuredHeight()/ ROW_NUMBER;
		//
		// AbsListView.LayoutParams param = new AbsListView.LayoutParams(
		// android.view.ViewGroup.LayoutParams.MATCH_PARENT,
		// height);
		// v.setLayoutParams(param);
		iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//UIHelper.startActivitys(mContext, data);
			}
		});

		return v;
	}

}
